define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/order_refund/index' + location.search,
                    add_url: 'litestore/order_refund/add',
                    edit_url: 'litestore/order_refund/edit',
                    del_url: 'litestore/order_refund/del',
                    multi_url: 'litestore/order_refund/multi',
                    import_url: 'litestore/order_refund/import',
                    table: 'litestore_order_refund',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //console.log(data);
                //这里我们手动设置底部的值
                $("#refund_amount").text(data.extend.refund_amount);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'order_id', title: __('Order_id')},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'goods.goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'goods.goods_attr', title: __('Goods_sku'), operate:  false},
                        // {field: 'order_goods_id', title: __('Order_goods_id')},
                        {field: 'refund_no', title: __('Refund_no'), operate: 'LIKE'},
                        {field: 'refund_money', title: __('Refund_money'), operate: false},
                        {field: 'refund_reason', title: __('Refund_reason'), operate: false},
                        {field: 'refund_type', title: __('Refund_type'), searchList: {"1":__('Refund_type 1'),"2":__('Refund_type 2')},custom: {"1": "info","2": "success"}, formatter: Table.api.formatter.label},
                        {
                            field: 'refund_status',
                            title: __('Refund_status'),
                            searchList: {"0":__('Refund_status 0'),"1":__('Refund_status 1'),"2":__('Refund_status 2'),"3":__('Refund_status 3'),"4":__('Refund_status 4'),"5":__('Refund_status 5')},
                            custom: {"0": "danger","1": "primary","2": "gray","3": "info","4": "warning","5": "success"},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'pass',
                                    text: __('Pass'),
                                    title: __('Pass'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-check',
                                    url: 'litestore/order_refund/pass',
                                    confirm: __('Are you sure to pass?'),
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.refund_status == 0 && row.refund_type == 1){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'pass',
                                    text: __('Pass'),
                                    title: __('Pass'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'litestore/order_refund/pass',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.refund_status == 0 && row.refund_type == 2){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'refuse',
                                    text: __('Refuse'),
                                    title: __('Refuse'),
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: 'fa fa-close',
                                    click: function (data, row) {
                                        layer.prompt({title: __('Please fill in the reason for rejection'), formType: 2}, function(text, index){
                                            Backend.api.ajax({
                                                url: "litestore/order_refund/refuse/ids/" + row.id,
                                                data: {remark:text}
                                            }, function (data,ret) {
                                                Layer.msg(ret.msg,{icon:1});
                                                table.bootstrapTable('refresh');
                                                Layer.close(index);
                                                return false;
                                            },function (data,ret) {
                                                Layer.msg(ret.msg,{icon:2});
                                                return false;
                                                //Layer.close(index);
                                            });
                                        });
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.refund_status == 0){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'complete',
                                    text: __('Confirm receipt'),
                                    title: __('Confirm receipt'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    icon: '',
                                    url: 'litestore/order_refund/complete',
                                    confirm: __('Confirm receipt?'),
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.refund_status == 4){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    title: __('Detail'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: '',
                                    url: 'litestore/order_refund/detail',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ],
                onLoadSuccess: function () {
                    $(".btn-dialog").data("area", ['60%','80%']);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        pass: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
